﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Hxj.Common;
using Hxj.Data;
using Hxj.Web;
using RicciWebsite.Model;

namespace RicciWebsite.admin
{
    public partial class adduser : RicciWebsite.Pages.AdminPage
    {
        private Guid? userid;

        protected void Page_Load(object sender, EventArgs e)
        {
            string tempuserid = Request.QueryString["userid"];

            if (Validator.IsGuid(tempuserid))
            {
                userid = new Guid(tempuserid);
            }

            if (!IsPostBack)
            {
                if (userid.HasValue)
                {
                    getUserData();
                    lltitle.Text = "修改用户信息";
                }


            }
        }

      

        /// <summary>
        /// 获取用户信息
        /// </summary>
        private void getUserData()
        {
            base_user user = RicciDatabase.Default.From<base_user>().Where(base_user._.userid == userid.Value.ToString()).ToFirst();

            if (null == user)
            {
                JavaScript.MsgBox_Redirect_End("该用户信息不存在!", "userlist.aspx");
                return;
            }

            txtUserName.Text = user.username;
            txtMemo.Text = user.memo;

            rfv2.Visible = false;
        }

        /// <summary>
        /// 提交
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnadd_Click(object sender, EventArgs e)
        {
            if (!IsValid)
                return;


            if (userid.HasValue)
            {
                base_user user = RicciDatabase.Default.From<base_user>().Where(base_user._.userid == userid.Value.ToString()).ToFirst();

                if (string.Compare(user.username.Trim(), txtUserName.Text.Trim(), true) != 0)
                {
                    base_user userto = RicciDatabase.Default.From<base_user>().Where(base_user._.username == txtUserName.Text.Trim()).ToFirst();

                    if (null != userto)
                    {
                        JavaScript.MsgBox("该用户名已存在!");
                        return;
                    }
                }
                user.Attach();
                user.memo = txtMemo.Text.Trim();
                user.username = txtUserName.Text.Trim();
                user.salt = StringHelper.GetRandomString();
                user.passwd = Password.Encode(txtPasswd.Text.Trim(), user.salt);

                int returnValue = RicciDatabase.Default.Update<base_user>(user);

                if (returnValue == 0)
                    JavaScript.MsgBox("操作超时,请重试!");
                else
                    JavaScript.MsgBox("修改成功!");

            }
            else
            {
                base_user user = RicciDatabase.Default.From<base_user>().Where(base_user._.username == txtUserName.Text.Trim()).ToFirst();

                if (null != user)
                {
                    JavaScript.MsgBox("该用户名已存在!");
                    return;
                }

                user = new base_user();
                user.createdate = DateTime.Now;
                user.memo = txtMemo.Text.Trim();
                user.userid = Guid.NewGuid().ToString();
                user.username = txtUserName.Text.Trim();
                user.status = 1;
                user.salt = StringHelper.GetRandomString();
                user.passwd = Password.Encode(txtPasswd.Text.Trim(), user.salt);

                int returnValue = RicciDatabase.Default.Insert<base_user>(user);

                if (returnValue == 0)
                    JavaScript.MsgBox("操作超时,请重试!");
                else
                    JavaScript.MsgBox_Redirect_End("用户添加成功!", "userlist.aspx");
            }

        }
    }
}
